package com.cn.ant.modules.score.web;

import com.cn.ant.common.persistence.Page;
import com.cn.ant.common.utils.AjaxJson;
import com.cn.ant.common.utils.StringUtils;
import com.cn.ant.common.web.BaseController;
import com.cn.ant.modules.score.entity.SchoolTwfInfo;
import com.cn.ant.modules.score.service.SchoolTwfInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 学校同位分Controller
 * @author 黄根华
 * @version 2015-10-09
 */
@Controller
@RequestMapping(value = "${adminPath}/score/schoolTwfInfo")
public class SchoolTwfInfoController extends BaseController {

	@Autowired
	private SchoolTwfInfoService schoolTwfInfoService;
	
	@ModelAttribute
	public SchoolTwfInfo get(@RequestParam(required=false) String id) {
		SchoolTwfInfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = schoolTwfInfoService.get(id);
		}
		if (entity == null){
			entity = new SchoolTwfInfo();
		}
		return entity;
	}
	
	@RequiresPermissions("score:schoolTwfInfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(SchoolTwfInfo schoolTwfInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SchoolTwfInfo> page = schoolTwfInfoService.findPage(new Page<SchoolTwfInfo>(request, response), schoolTwfInfo); 
		model.addAttribute("page", page);
		return "modules/score/schoolTwfInfoList";
	}

	@RequiresPermissions("score:schoolTwfInfo:view")
	@RequestMapping(value = "form")
	public String form(SchoolTwfInfo schoolTwfInfo, Model model) {
		model.addAttribute("schoolTwfInfo", schoolTwfInfo);
		return "modules/score/schoolTwfInfoForm";
	}

	@RequiresPermissions("score:schoolTwfInfo:edit")
	@RequestMapping(value = "save")
	@ResponseBody
	public AjaxJson save(SchoolTwfInfo schoolTwfInfo, HttpServletRequest request,Model model) {
		AjaxJson jo = new AjaxJson();
		try {
			int year = Integer.parseInt(request.getParameter("year"));
			schoolTwfInfoService.init(year);
			msg = "初始化学校同位分成功";
			jo.setMsg(msg);
			jo.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
            jo.setSuccess(false);
            jo.setMsg("操作失败,请联系管理员");
		}
		return jo;
	}
	
	@RequiresPermissions("score:schoolTwfInfo:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public AjaxJson delete(SchoolTwfInfo schoolTwfInfo, RedirectAttributes redirectAttributes) {
		AjaxJson jo = new AjaxJson();
        if (StringUtils.isNotBlank(schoolTwfInfo.getId())) {
            schoolTwfInfoService.delete(schoolTwfInfo);
            jo.setMsg("删除学校同位分成功");
        } else {
            jo.setSuccess(false);
            jo.setMsg("参数错误,删除失败");
        }
        return jo;
	}

}